package logic

import (
	"context"
	"go-zero-demo/distributed-id-rpc/distributed"
	"go-zero-demo/distributed-id-rpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetIdLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGetIdLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetIdLogic {
	return &GetIdLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *GetIdLogic) GetId(in *distributed.IdReq) (*distributed.IdResp, error) {
	useCase := l.svcCtx.SegmentUseCase
	out := &distributed.IdResp{}
	id, err := useCase.GetId(l.svcCtx.DBEngin, in.Tag)
	if err != nil {
		return nil, err
	}
	out.Id = id
	return out, nil
}
